#include "Trade.h"

unsigned int Trade::last_TradeID = 11000;

void Trade::addItem(Item _item) {
	for (size_t i = 0; i < items.size(); i++) {
		if (items[i] == _item)
			throw ExistentItem();
	}

	items.push_back(_item);
}

void Trade::save(string filename, ofstream & out) {
	//ofstream out;
	//out.open(filename.c_str(), fstream::app);

	out << "[TRANS]" << endl;

	out << this->getDate().get_day() << endl;
	out << this->getDate().get_month() << endl;
	out << this->getDate().get_year() << endl;
	out << this->getDate().get_hour() << endl;
	out << this->getDate().get_minute() << endl;

	if (this->getHasEnded() == true) {
		out << this->getClientEnder() << endl; //clientID or owner
		out << "TRUE" << endl;
	} else {
		out << this->getClientEnder() << endl; //NULL
		out << "FALSE" << endl;
	}

	out << "[ITEMS]" << endl;

	for (size_t j = 0; j < items.size(); j++) {
		out << items[j].getID() << endl;
	}

	out << "[/ITEMS]" << endl;

	out << this->getClientStarter() << endl;
	out << "TRADE" << endl;
	out << "[/TRANS]" << endl;
}

void Trade::showTransaction() {
	cout << "Trade - ID: " << tradeID << endl;

	if (starterID == "NULL") {//is this supposed to happen?
		cout << "No starter" << endl;
	} else {
		cout << "Starter ID: " << starterID << endl;
	}
	if (enderID == "NULL") {
		cout << "No one accepted the trade yet" << endl;
	} else {
		cout << "Ender ID: " << enderID << endl;
	}
	cout << "------------------------------------------------------------"
				<< endl;
	for (size_t i = 0; i < items.size(); ++i) {
		items[i].showItem();
	}
	cout << "------------------------------------------------------------"
			<< endl;
}
